package com.burgeon.backend.SHOPBACKEND.rest.openCardTask;
/**
 * 开卡任务头部数据
 */

import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class TopDataStore extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public TopDataStore(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public ArrayList<HashMap<String,String>> getData(String date) throws Exception {
        String url = "http://"+ip+"/servlets/binserv/Rest";

        String str = RESULT(Data(date),url);
        System.err.println(str);
        JSONObject jsonObject = new JSONArray(str).getJSONObject(0);
        String code = jsonObject.getString("code");

        String message = jsonObject.getString("message");
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<HashMap<String,String>> data = new ArrayList< HashMap<String,String>>();

        for (int i = 0; i < jsonArray.length(); i++) {
            HashMap<String,String> bean = new HashMap<String,String>();
            bean.put("name",jsonArray.getJSONArray(i).getString(1));
            bean.put("tot",jsonArray.getJSONArray(i).getString(2));
            data.add(bean);
        }

        return data;
    }

    private MultiValueMap Data(String date) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("'C_STORE_ID;NAME'");
        columns.add("TOT_OPENVIP");

        params.put("columns", columns);
        params.put("table", "C_STOREODMARK");//表名


        //设置查询规则（从第几条查询，每页显示多少条数）
        params.put("range", 10);
        params.put("count", "true");
        params.put("start", 0);

        //设置排序规则
        HashMap orderby = new HashMap();
        orderby.put("column", "TOT_OPENVIP");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        HashMap paramss = new HashMap();
        String ac = "'"+ date +"'";
        paramss.put("column", "'YEARMONTH'");
        paramss.put("condition", ac);
        params.put("params", paramss);



        return DATA("Query", params,account,password);
    }

    public static void main(String[] args) {
        TopDataStore getRetailByVip = new TopDataStore("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(getRetailByVip.getData("202102"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
